from ctaf_core.ctaf_factory import CtafFactory
from ctaf_core.ctaf_objects.base_biz_app import BaseGatewayApp
from ctaf_core.ctaf_objects.base_ctaf_service import BaseCTAFservice
from ctaf_core.ctaf_objects.base_service_request import BaseServiceReq
from ctaf_core.ctaf_objects.ctaf_outbound_request import SubscribeRequest, OrderRequest, CancelRequest, QuoteRequest, \
    HistoryRequest
from ctaf_core.ctaf_platform import CoreTradeAppFramework


class ConnectReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,setting: dict, gateway_name: str):
        super().__init__(rpc_service_name)
        self.setting: dict = setting
        self.gateway_name = gateway_name

class SubscribeReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: SubscribeRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: SubscribeRequest = req
        self.gateway_name = gateway_name


class SendOrderReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: OrderRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: OrderRequest = req
        self.gateway_name = gateway_name


class CancelOrderReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: CancelRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: CancelRequest = req
        self.gateway_name = gateway_name


class SendQuoteReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: QuoteRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: QuoteRequest = req
        self.gateway_name = gateway_name

class CancelQuoteReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: CancelRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: CancelRequest = req
        self.gateway_name = gateway_name


class QueryHistoryReq(BaseServiceReq):

    def __init__(self, rpc_service_name: str,req: HistoryRequest, gateway_name: str):
        super().__init__(rpc_service_name)
        self.req: HistoryRequest = req
        self.gateway_name = gateway_name



class GatewayRpcServer(BaseCTAFservice):

    def connect(self, connectReq: ConnectReq) -> None:
        """
        Start connection of a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(connectReq.gateway_name)
        if gateway:
            gateway.connect(connectReq.setting)

    def subscribe(self, subscribeReq: SubscribeReq) -> None:
        """
        Subscribe tick data update of a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(subscribeReq.gateway_name)
        if gateway:
            gateway.subscribe(subscribeReq.req)

    def send_order(self, sendOrderReq: SendOrderReq) -> str:
        """
        Send new order request to a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(sendOrderReq.gateway_name)
        if gateway:
            return gateway.send_order(sendOrderReq.req)
        else:
            return ""

    def cancel_order(self, cancelOrderReq: CancelOrderReq) -> None:
        """
        Send cancel order request to a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(cancelOrderReq.gateway_name)
        if gateway:
            gateway.cancel_order(cancelOrderReq.req)

    def send_quote(self, req: SendQuoteReq) -> str:
        """
        Send new quote request to a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(req.gateway_name)
        if gateway:
            return gateway.send_quote(req.req)
        else:
            return ""

    def cancel_quote(self, req: CancelRequest, gateway_name: str) -> None:
        """
        Send cancel quote request to a specific gateway.
        """
        ctaf : CoreTradeAppFramework = CtafFactory.get_ctaf_instance()
        gateway: BaseGatewayApp = ctaf.get_biz_app_by_name(gateway_name)
        if gateway:
            gateway.cancel_quote(req)

